<!DOCTYPE html>

<html>
	<head>
		<meta charset="utf-8" />
		<link rel="stylesheet" type="text/css" href="style.css" />
		<title>Catégories</title>
	</head>

	<body>
		<?php if (!isset($_SESSION))
				session_start(); ?>
		<div class="top-menu">
		<ul>
			<li><a href="index.php" >Accueil</a></li>
			<li><a href="affCategorie.php">Catégories</a></li>
		</ul>
		
		<?php
		if (!isset($_SESSION) || !isset($_SESSION['connected']) || !$_SESSION['connected'] == true)
		{
			?>
			<script type="text/javascript">
				document.location.href="index.php";
			</script>
		<div class="login-box">
		<form action="connexion.php" method="post">
			Connexion :<input type="text" name="login" placeholder="Login" />
			<input type="text" name="mdp" placeholder="Password" />
			<input type="submit" value="OK">
		</form>
		</div>
		<?php
		}
		else
		{
			?>
			<div class="login-box">Vous etes connecté en tant que <?php
			if (isset($_SESSION) && isset($_SESSION["login"]))
				echo '<div class="login-name">' . $_SESSION["login"] . '</div>';
			?>
			<a href="sdestr.php">Déconnexion</a>
			</div>
			<?php
		}
		?>
		<br />
	</div>
	
		<?php

			//session_start();

			if (isset($_GET['page']))
				{
					$page = $_GET['page'];
				}
			else 
				{
					$page = 1;
				}

			$mysqli = new mysqli("localhost", "root", "root", "forum");
			
			/* Vérification de la connexion */
			if (mysqli_connect_errno())
				{
    				echo 'Échec de la connexion : ' . mysqli_error();
    				exit;
				}

			$requete = 'SELECT * FROM categorie';
			
			if ($response = $mysqli->query($requete))
				{
					$cpt = 0;
					while ($row = $response->fetch_assoc())
						{
							$arrayToAff[$cpt] = $row;
							$cpt++;
						}
					$maxPage = ($cpt / 20) + 1;
					$cpt = 0;
					$toAffMin = ($page - 1) * 20;
					$toAffMax = ($page * 20) - 1;
					$toAff = $toAffMin;
					while (isset($arrayToAff[$toAff]) && $arrayToAff[$toAff] != null && $cpt < 20)
						{
							echo '<div class="core-design">';
							echo '<a href="affTopic.php?idCat=' . $arrayToAff[$toAff]["id_cat"] . '"><p> Title : ' . $arrayToAff[$toAff]['titre_cat'] . '</p></a>';
							echo '<p> Description : ' . $arrayToAff[$toAff]['description_cat'] . '</p>';
							echo '</div>';
							$cpt++;
							$toAff++;
						}
					$cpt = 1;


					if ($_SESSION["isAdmin"] == true)
					{
						echo '<div class="core-design"><form action="putCategorie.php" method="post">
								<p>Titre : <input type="text" name="titre" /></p>
								<p>Description : <input type="text" name="description" /></p>
								<p><input type="submit" value="OK"></p>
								</form></div>';
					}					
					
					echo '<footer>Page' . $page . ' / ' . intval($maxPage) . ' ';
					while ($cpt < $maxPage)
					{
						echo '<a href="affCategorie.php?page=' . $cpt . '">' . $cpt . ' </a>';
						$cpt++;
					}
					echo '</footer>';
				}
		
		?>
	</body>
</html>